import React from 'react';
import {BrowserRouter, Route, Redirect} from 'react-router-dom';
import {InitComs} from '../common/PublicComponent';
import {getCurrentLoginUser} from '../common/permission';
import {renderRoutes} from 'react-router-config';
import {commontLimtRouter} from './common.config.js';
import BreadCrumbComs from '../components/BreadCrumbComs';
import Bundle from './DynamicRoute';
import Frame from '../frame/Frame';
import Home from '../pages/home/Home';
import Content from '../pages/admin/frame/index';


commontLimtRouter.forEach(r => {
    const oriOnEnter = r.onEnter;
    const oriOnLeave = r.onLeave;

    r.onEnter = (nextState, replace, callback) => {
        onEnter(nextState, replace, callback, oriOnEnter);
    };
    r.onLeave = (prevState) => {
        onLeave(prevState, oriOnLeave);
    };
});

function onLeave(prevState, oriOnLeave) {
    console.log('le');
    if (oriOnLeave) {
        oriOnLeave(prevState);
    }
}

function onEnter(nextState, replace, callback, oriOnEnter) {
    console.log(nextState, 'next');
    if (oriOnEnter) {
        oriOnEnter(nextState, replace, callback);
    } else {
        const scrollDom = document.documentElement || document.body;
        scrollDom.scrollTop = 0;
        callback();
    }
}

const routes = [
    {
        path: '/',
        component: ({route}) => {
            return (<div>
                {renderRoutes(route.routes, {someProp: 'these extra props are optional'})}
            </div>);
        },
        routes: [...commontLimtRouter,]
    }
]

function changeFrame() {
    if (window.location.pathname.includes('view+') || window.location.pathname === '/') {
        return <Frame>{renderRoutes(routes)}</Frame>;
    } else if (window.location.pathname.includes('admin') || window.location.pathname === '/') {
        console.log(getCurrentLoginUser(), 'current');
        if (getCurrentLoginUser()) {
            return <Content>{renderRoutes(routes)}</Content>;
        } else {
            window.location.href = '/login';
        }
    }
    return renderRoutes(routes);
}

export default (
    <div>
        <BrowserRouter basename={''}>
            <div>
                <InitComs/>
                {/*<MenuComs/>*/}
                {window.location.pathname === '/test' && <BreadCrumbComs routes={commontLimtRouter}/>}
                {/* (window.location.pathname.includes('view+') || window.location.pathname === '/') ? <Frame>{renderRoutes(routes)}</Frame> : ((window.location.pathname.includes('admin+') || window.location.pathname === '/') ? <Content>{renderRoutes(routes)}</Content> : renderRoutes(routes)) */}
                {changeFrame()}
                {/*路由配置*/}
            </div>
        </BrowserRouter>
    </div>
)

const PrivateRoute = ({component: Component, ...rest}) => (
    <Route {...rest} render={props => (
        isLogin ? (
            <Component {...props}/>
        ) : (
            <Redirect to={{
                pathname: '/login',
                state: {from: props.location}
            }}/>
        )
    )}/>
)



